package cn.skycity.common.controller;

import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.ISysDictDataService;
import cn.skycity.framework.core.model.Result;
import cn.skycity.framework.core.utils.LangUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skycity.common.log.annotation.Log;
import com.skycity.common.log.enums.BusinessType;
import com.skycity.framework.uaa.pojo.entity.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YingBo.Dai
 * @since 2024-12-13
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController {
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 字典项管理
     **/
    @GetMapping("/page")
    public Result<Page<SysDictData>> pageDictItemList(Page<SysDictData> pageVo, SysDictData sysDictItem) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(StrUtil.isNotBlank(sysDictItem.getDictType()), SysDictData::getDictType, sysDictItem.getDictType())
                .and(StrUtil.isNotBlank(sysDictItem.getDictLabel()), i -> i.like(SysDictData::getDictLabel, sysDictItem.getDictLabel())
                        .or()
                        .like(SysDictData::getDictValue, sysDictItem.getDictLabel()))
                .orderByDesc(SysDictData::getDictType)
                .orderByAsc(SysDictData::getSort);
        Page<SysDictData> result = sysDictDataService.page(pageVo, queryWrapper);
        return Result.success(result);
    }

    /**
     * 根据code获取字典项
     *
     * @param dictType
     * @return
     */
    @GetMapping("/type/{dictType}")
    public Result<List<SysDictData>> getDictItemList(@PathVariable(value = "dictType", required = false) String dictType) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<SysDictData>()
                .like(StrUtil.isNotBlank(dictType), SysDictData::getDictType, dictType)
                .orderByAsc(SysDictData::getSort);
        return Result.success(sysDictDataService.list(queryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysDictData> getInfo(@PathVariable(value = "id", required = false) Long id) {
        return Result.success(sysDictDataService.getById(id));
    }

    @PostMapping
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    public Result<?> addDictItem(@RequestBody SysDictData sysDictData) {
        long count = sysDictDataService.count(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getDictValue, sysDictData.getDictValue()));
        if (count > 0) {
            return Result.failed(LangUtil.getMessage("dict.code.exist"));
        }
        sysDictDataService.save(sysDictData);
        return Result.success();
    }

    @PutMapping
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    public Result<?> updateDictItem(@RequestBody SysDictData sysDictData) {
        long count = sysDictDataService.count(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getDictValue, sysDictData.getDictValue()).ne(SysDictData::getId, sysDictData.getId()));
        if (count > 0) {
            return Result.failed(LangUtil.getMessage("dict.code.exist"));
        }
        sysDictDataService.updateById(sysDictData);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public Result<?> deleteDictItem(@PathVariable("id") String ids) {
        List<Long> idArr = Stream.of(ids.split(",")).map(Long::parseLong).toList();
        sysDictDataService.removeByIds(idArr);
        return Result.success();
    }

}
